package com.longrise.android.byjk.plugins.poster;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.PosterShareResultEvent;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.poster.adapter.PosterFragmentAdapter;
import com.longrise.android.byjk.plugins.poster.fragment.PosterListFragment;
import com.longrise.android.byjk.plugins.poster.posterdetail.assist.AccessCount;
import com.longrise.android.byjk.plugins.poster.posterdetail.assist.ImgList;
import com.longrise.android.byjk.plugins.publicWeb.PublicWebActivity;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DZZWTools;
import com.longrise.common.utils.PrintLog;
import com.longrise.common.utils.UmengStatisticsUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;


/**
 * Created by godliness on 2017/9/25.
 * 海报列表
 */

public class PosterListActivity extends BaseActivity2<PosterListPresenter> implements PosterListView {

    public static final String TAG = "PosterListActivity";

    private TabLayout tl_poster;
    private ViewPager vp_poster;
    private PosterFragmentAdapter adapter;
    private ArrayList<Fragment> list = new ArrayList<>();
    private ArrayList<String> areaList = new ArrayList<>();
    private Toolbar mTitle;
    private PosterListFragment posterListFragment;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_poster_list;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter.request();
        mPresenter.getUserIconBitmap();

        UmengStatisticsUtil.onEvent("poster_genre" + (1)); //进页面默认显示第一页
    }

    @Override
    public void initView() {

        tl_poster = (TabLayout) findViewById(R.id.tl_poster);
        vp_poster = (ViewPager) findViewById(R.id.vp_poster);
        initTitle();
        tl_poster.setupWithViewPager(vp_poster);

        mPresenter.getTabData();

        vp_poster.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                tl_poster.setScrollPosition(position, positionOffset, true);
            }

            @Override
            public void onPageSelected(int position) {
                UmengStatisticsUtil.onEvent("poster_genre" + (position + 1));
            }
        });

        tl_poster.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp_poster.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                vp_poster.setCurrentItem(tab.getPosition());
            }
        });

        EventBus.getDefault().register(this);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void initTitle() {
        setToolbarTitle(AppUtil.getString(R.string.poster));
    }


    /**
     * 监控关闭了 分享详情页面 将分享数量传给服务器
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void regShareDetailClose(PosterShareResultEvent event) {
        if (event.isClosePage()) {
            EntityBean bean = AccessCount.getInstance().getAccessBean();
            mPresenter.updateReadStatus(bean);
            EntityBean bean1 = AccessCount.getInstance().getShareBean();
            mPresenter.updateShareStatus(bean1);
        }
    }

    /**
     * 分享的结果
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void regShareResult(PosterShareResultEvent event) {
        String shareRes = event.getShareRes();
        if (TextUtils.isEmpty(shareRes)) {
            return;
        }
        View v = View.inflate(AppUtil.getContext(), R.layout.toast_share, null);
        TextView tv = (TextView) v.findViewById(R.id.tv_share_msg);
        ImageView iv_icon = (ImageView) v.findViewById(R.id.iv_icon);
        switch (shareRes) {
            case "0":
                iv_icon.setImageResource(R.drawable.bg_toast_success);
                tv.setText("分享成功");
                DZZWTools.showToast(v, 0, Gravity.CENTER, null);
                break;

            case "1":
                iv_icon.setImageResource(R.drawable.bg_toast_fail);
                tv.setText("分享失败");
                DZZWTools.showToast(v, 0, Gravity.CENTER, null);
                break;

            case "2":
                iv_icon.setImageResource(R.drawable.bg_toast_fail);
                tv.setText("分享取消");
                DZZWTools.showToast(v, 0, Gravity.CENTER, null);
                break;
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void showData(EntityBean[] beans) {
        if (beans == null) return;
        ImgList.getInstance().clearAll();
        if (beans.length <= 3) {
            tl_poster.setTabMode(TabLayout.MODE_FIXED);
            tl_poster.setTabGravity(TabLayout.GRAVITY_FILL);
        } else {
            tl_poster.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
        for (int i = 0; i < beans.length; i++) {
            String goodstype = beans[i].getString("goodstype");
            String areaname = beans[i].getString("areaname");
            if (i == 0) {
                posterListFragment = PosterListFragment.newInstance(goodstype, areaname, 0);
                list.add(posterListFragment);
            } else {
                posterListFragment = PosterListFragment.newInstance(goodstype, areaname, 1);
                list.add(posterListFragment);
            }
            areaList.add(areaname);
        }
        adapter = new PosterFragmentAdapter(getSupportFragmentManager(), list, areaList);
        vp_poster.setAdapter(adapter);
        tl_poster.setupWithViewPager(vp_poster);
        vp_poster.setCurrentItem(0);
        vp_poster.setOffscreenPageLimit(list.size());

    }

    @Override
    public void setFeedBackUrl(String url) {
        StringBuilder mBuilder = new StringBuilder();
        mBuilder.setLength(0);
        mBuilder.append(url);
        mBuilder.append("?cardno=");
        mBuilder.append(UserInfor.getInstance().getUsersfzh());
        mBuilder.append("&mobilephone=");
        mBuilder.append(UserInfor.getInstance().getUserphone());
        mBuilder.append("&longinname=");
        mBuilder.append(UserInfor.getInstance().getUserName());
        mBuilder.append("&area=");
        mBuilder.append(UserInfor.getInstance().getUserworkareaname());
        String fullUrl = mBuilder.toString();
        Intent intent = new Intent(PosterListActivity.this, PublicWebActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(PublicWebActivity.TITLE, "我要提意见");
        bundle.putString(PublicWebActivity.URL, fullUrl);
        bundle.putString(PublicWebActivity.BACK_TYPE, PublicWebActivity.BACK_ARROW);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
